package com.ruoyi.system.mq;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

@Component
public class Producer {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    public boolean sendMessage(String str){ // 要发送的消息
        Message<String> message = MessageBuilder.withPayload(str).build();// 构建消息
        SendResult sendResult = rocketMQTemplate.syncSend("test-topic:test-tag", message,2000,1);
        SendStatus sendStatus = sendResult.getSendStatus(); // 获取消息状态   是否成功
        return SendStatus.SEND_OK == sendStatus?true:false; // 返回
    }

}
